#include<stdio.h>
#include<conio.h>
int getCount(int n,int m){
    int count=0,k=m;
    while(n>1 && m>1){
        if(m%2==0){
            m=m/2-1;
        }else m=m/2;
        count++;
        n--;
    }
    if(n==1)return count+m;
    else return count;
}
int main(){
    int test,i,m,n;
    scanf("%d",&test);
    for(i=1;i<=test;i++){
        if(i!=1)printf("\n");
        scanf("%d%d",&n,&m);
        printf("%d",getCount(n,m));
    }
    getch();
    return 0;
}
